import {
  AppItem,
  AuthParam,
  AuthPolicyItem,
  AuthTableItem,
  AuthTableParam,
  BatchAddParam,
  CancelAuthParam,
  CompoItem,
  CreateUserGroupParams,
  DeleteUserGroupParam,
  DutyDurationItem,
  EditUserGroupParams,
  FastScheduleItem,
  FastScheduleParam,
  GroupListItem,
  UpdateItem,
  UpdateParam,
  UserGroupTableItem,
  UserGroupTableParams,
  UserGroupTableRecord,
  WorksheetUserItem,
} from './model/userGroupModel';
import { workOrderHttp } from '/@/utils/http/axios';

enum Api {
  GET_USER_GROUP_TABLE = '/v1/groupManager/page',
  EDIT_USER_GROUP = '/v1/groupManager/update',
  CREATE_USER_GROUP = '/v1/groupManager/create',
  DELETE_USER_GROUP = '/v1/groupManager',
  GET_APP_LIST = '/v1/groupManager/appList',
  AUTHORIZE_USER_GROUP = '/v1/groupManager/auth',
  CANCEL_AUTHORIZE = '/v1/groupManager/deleteAuth',
  GET_COMPO_LIST = '/v1/groupManager/componentList',
  GET_AUTH_TABLE = '/v1/groupManager/groupAuthorityPage',
  GET_AUTH_POLICY = '/v1/groupManager/policyList',
  GET_DETAIL = '/v1/groupManager',
  BATCH_INCREASE = '/v1/groupManager/',
  GET_GROUP_LIST = '/v1/groupManager/groupList',
  GET_WORKSHEET_USER = '/v1/groupManager/worksheetUserList',
  UPDATE_SCHEDULE = '/v1/schedule/updateSchedule',
  GET_DUTYDURATION = '/v1/schedule/getScheduleEnum',
  GET_FAST_SCHEDULE = '/v1/schedule/getSchedulePreview',
}

export const getWorksheetUser = async (params?: DeleteUserGroupParam) => {
  const res = await workOrderHttp.get<WorksheetUserItem[]>({
    url: Api.GET_WORKSHEET_USER,
    params,
  });
  res.map((item) => {
    return (item.key = String(item.id));
  });
  return res;
};

export const getAuthPolicyList = async () => {
  const res = await workOrderHttp.get<AuthPolicyItem[]>({
    url: Api.GET_AUTH_POLICY,
  });
  res.map((item) => {
    item.label = item.name;
    item.value = item.id;
    return item;
  });
  return res;
};

export const getGroupList = async () => {
  const res = await workOrderHttp.get<GroupListItem[]>({
    url: Api.GET_GROUP_LIST,
  });
  res.map((item) => {
    item.label = item.name;
    item.value = item.id;
    return item;
  });
  return res;
};

export const getUserGroupTable = async (params: UserGroupTableParams) => {
  const res = await workOrderHttp.post<UserGroupTableItem>({
    url: Api.GET_USER_GROUP_TABLE,
    params,
  });
  res.records.map((item) => {
    return (item.isScheduledStr = item.isScheduled ? '已排班' : '未排班');
  });
  return res;
};

export const editUserGroup = async (params: EditUserGroupParams) => {
  const res = await workOrderHttp.put<string>({
    url: Api.EDIT_USER_GROUP,
    params,
  });
  return res;
};

export const createUserGroup = async (params: CreateUserGroupParams) => {
  const res = await workOrderHttp.post<string>({
    url: Api.CREATE_USER_GROUP,
    params,
  });
  return res;
};

export const deleteUserGroup = async (params: DeleteUserGroupParam) => {
  const res = await workOrderHttp.delete<string>({
    url: Api.DELETE_USER_GROUP,
    params,
  });
  return res;
};

export const getAppList = async () => {
  const res = await workOrderHttp.get<AppItem[]>({
    url: Api.GET_APP_LIST,
  });
  res.map((item) => {
    return {
      label: item.appName,
      value: item.appId,
    };
  });
  return res;
};

export const getCompoList = async (params: number[]) => {
  const res = await workOrderHttp.post<CompoItem[]>({
    url: Api.GET_COMPO_LIST,
    params,
  });
  res.map((item) => {
    return {
      label: item.name,
      value: item.id,
    };
  });
  return res;
};

export const authorizeUserGroup = async (params: AuthParam) => {
  const res = await workOrderHttp.post<string>({
    url: Api.AUTHORIZE_USER_GROUP,
    params,
  });
  return res;
};

export const cancelAuthorize = async (params: CancelAuthParam) => {
  const res = await workOrderHttp.post<string>({
    url: Api.CANCEL_AUTHORIZE,
    params,
  });
  return res;
};

export const getAuthTable = async (params: AuthTableParam) => {
  const res = await workOrderHttp.post<AuthTableItem>({
    url: Api.GET_AUTH_TABLE,
    params,
  });
  return res;
};

export const getDetail = async (params: DeleteUserGroupParam) => {
  const res = await workOrderHttp.get<UserGroupTableRecord>({
    url: Api.GET_DETAIL,
    params,
  });
  res.isScheduledStr = res.isScheduled ? '已排班' : '未排班';
  return res;
};

export const batchAddUser = async (params: BatchAddParam) => {
  const res = await workOrderHttp.put<string>({
    url: Api.BATCH_INCREASE + params.groupId + '/add',
    params: params.userIdSet,
  });
  return res;
};

export const reallocateUser = async (params: BatchAddParam) => {
  const res = await workOrderHttp.put<string>({
    url: Api.BATCH_INCREASE + params.groupId + '/reallocate',
    params: params.userIdSet,
  });
  return res;
};

export const batchRemoveUser = async (params: BatchAddParam) => {
  const res = await workOrderHttp.put<string>({
    url: Api.BATCH_INCREASE + params.groupId + '/remove',
    params: params.userIdSet,
  });
  return res;
};

export const updateSchedule = async (params: UpdateParam) => {
  const res = await workOrderHttp.put<UpdateItem>({
    url: Api.UPDATE_SCHEDULE,
    params,
  });
  return res;
};

export const getDutyDuration = async () => {
  const res = await workOrderHttp.get<DutyDurationItem[]>({
    url: Api.GET_DUTYDURATION,
  });
  res.map((item) => {
    return {
      label: item.description,
      value: item.duration,
    };
  });
  return res;
};

export const getFastSchedule = async (params: FastScheduleParam) => {
  const res = await workOrderHttp.post<FastScheduleItem>({
    url: Api.GET_FAST_SCHEDULE,
    params,
  });
  return res.schedulePreview;
};
